package net.zhengxinyun.unifyApp.service.message;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.unifyApp.Util.DateUtils;
import net.zhengxinyun.unifyApp.Util.StringUtils;
import net.zhengxinyun.unifyApp.Util.SystemApiUtl;
import net.zhengxinyun.unifyApp.Util.httpsUitl.HttpClientService;
import net.zhengxinyun.unifyApp.entity.message.OperaInfo;
import net.zhengxinyun.unifyApp.filter.message.OperaInfoFilter;
import net.zhengxinyun.unifyApp.mapper.message.OperaInfoMapper;
import org.apache.http.NameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Service
public class OperaInfoServiceImpl implements OperaInfoService {

    @Autowired
    OperaInfoMapper operaMapper;

    @Override
    public List<OperaInfo> getOperaInfo(OperaInfoFilter filter) {
        return operaMapper.getOperaInfo(filter);
    }


    @Override
    public int saveOperaInfo() {
        /***
         * 重大
         */
        String updInfo = "";
        String address = SystemApiUtl.SERVICEADDRESS;
        try {

            Object[] param = new Object[]{"null"};
            Object[] value = new Object[]{"null"};

            List<NameValuePair> nameValuePairs = HttpClientService.getParam(param, value);

            JSONObject ZdObject = HttpClientService.sendGet(address + ":8099/zhApp/leader/queryOperationLogEffect", nameValuePairs, "http");
            if (ObjectUtils.isNotBlank(ZdObject)) {
                JSONArray zdDataArray = ZdObject.getJSONArray("data");
                for (int i = 0; i < zdDataArray.size(); i++) {

                    JSONObject dataObj = zdDataArray.getJSONObject(i);
                    String msg = dataObj.getString("updInfo");
                    updInfo = updInfo + msg + ";";

                }

            }
            /**
             * 督查更新
             */
            String duChaParam = "{}";
            JSONObject duchaObj = HttpClientService.sendPost(address + ":8077/appAdmin/homeInfo", duChaParam, "http");
            System.out.println(duchaObj);
            if (ObjectUtils.isNotBlank(duchaObj)) {

                JSONObject duchaData = duchaObj.getJSONObject("data");
                String taskUnit = duchaData.getString("taskUnit");
                String taskBack = duchaData.getString("taskBack");
                String smsLog = duchaData.getString("smsLog");
                updInfo = updInfo + "督查系统一共发出" + taskUnit + "单位任务。;督查系统一共收到" + taskBack + "条反馈信息。;督查系统一共发出" + smsLog + "条督查短信。;";
            }

            OperaInfo operaInfo = new OperaInfo();
            operaInfo.setDatetime(DateUtils.getCurrentDate());
            operaInfo.setUpdTitle("综合政务运行成效数据更新");
            operaInfo.setUpdInfo(updInfo);

            int i = operaMapper.saveOperaInfo(operaInfo);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }


        return 0;
    }
}
